package atguigu2.queue;

import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @AUTHOR LYF
 * @DATE 2022/2/4
 * @VERSION 1.0
 * @DESC
 */
public class BlockingQueueDemo {
    public static void main(String[] args) {
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);

        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                try {
                    blockingQueue.put("data"+new Random().nextInt(99));
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"PutThread"+i).start();
        }


        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                try {

                    System.out.println( blockingQueue.take());
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            },"TakeThread"+i).start();
        }







    }
}
